#include "MenuButton.h"
#include "stdio.h"
#include "d3dapplication.h"
#include "SoundManager.h"

MenuButton::MenuButton(void)
{
	
}

MenuButton::~MenuButton(void)
{
	// release the textures
	mTextureLit->Release();
	mTextureUnlit->Release();
}

MenuButton::MenuButton(ButtonType id, float xLeft, float xRight, float yTop, float yBottom, int w, int h, LPCSTR texLit, LPCSTR texUnlit, ID3DXSprite* sprite, ID3DXFont* font)
{
	idNum = id;
	
	mLeft = xLeft;
	mRight = xRight;
	mTop = yTop;
	mBottom = yBottom;
	mHeight = (float)h;
	mWidth = (float)w;

	mCenter = D3DXVECTOR3((float)w/2, (float)h/2, 0.0f);
	mFont = font;
	mSprite = sprite;
	
	// create the textures
	D3DXCreateTextureFromFileEx(gD3DDevice, texLit, w, h, 1, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), 0, NULL, &mTextureLit);
	D3DXCreateTextureFromFileEx(gD3DDevice, texUnlit, w, h, 1, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), 0, NULL, &mTextureUnlit);

	mHighlighted = false;
	mActivated = true;
}

bool MenuButton::IsMouseOver(POINT mouspos)
{
	/*
	float x1 = screenX - mCenter.x;
	float y1 = screenY - mCenter.y;
	float x2 = screenX + mCenter.x;
	float y2 = screenY + mCenter.y;

	*/

	float x1 = mScreenW * mLeft;
	float x2 = mScreenW * mRight;
	float y1 = mScreenH * mTop;
	float y2 = mScreenH * mBottom;

	if (mouspos.x > x1 &&
		mouspos.x < x2 &&
		mouspos.y > y1 &&
		mouspos.y < y2 )
	{
		return true;
	}
	else
	{
	
		return false;
	}	
}

void MenuButton::Render()
{
	// renders the button based on the current state and displays text (format
	// given by the passed text object.
	// assumes the D3D rendering process has already begun

	if (!mActivated)
		return;

	D3DXVECTOR3 pos;
	pos.x = (mRight+mLeft) * mScreenW * 0.5f;
	pos.y = (mTop+mBottom) * mScreenH * 0.5f;
	pos.z = 0.0f;

	D3DXCOLOR textcolor;

	D3DXMATRIX T, S;

	D3DXMatrixTranslation(&T, pos.x, pos.y, pos.z);

	float xScale = mScreenW/mWidth * (mRight-mLeft);
	float yScale = mScreenH/mHeight * (mBottom-mTop);

	D3DXMatrixScaling(&S, xScale, yScale, 1.0f);

	mSprite->SetTransform(&(S*T));

	mSprite->Begin(D3DXSPRITE_ALPHABLEND);
	if(mHighlighted)
	{
		textcolor = TEXTCOLOR_PRESSED;	
		//	mSprite->Draw(mTextureLit, 0, &mCenter, &pos, D3DCOLOR_ARGB(255, 255, 255, 255));
		mSprite->Draw(mTextureLit, 0, &mCenter, 0, D3DCOLOR_ARGB(255, 255, 255, 255));
	}
	else
	{
		textcolor = TEXTCOLOR_UNPRESSED;
		//mSprite->Draw(mTextureUnlit, 0, &mCenter, &pos, D3DCOLOR_ARGB(255, 255, 255, 255));
		mSprite->Draw(mTextureUnlit, 0, &mCenter, 0, D3DCOLOR_ARGB(255, 255, 255, 255));
	}

	
	
	RECT R = {0, 0, 0, 0};

	// change the scaling part of the matrix for text rendering
	xScale = mScreenW/mWidth * (mRight-mLeft);
	yScale = mScreenH/mHeight * (mBottom-mTop);

	//D3DXMatrixScaling(&S, xScale, yScale, 1.0f);

	//mSprite->SetTransform(&(S*T));

	mFont->DrawTextA(mSprite, mTextBuffer, -1, &R, DT_NOCLIP | DT_CENTER | DT_VCENTER, textcolor);
	mSprite->End();
}

void MenuButton::OnResetDevice(float bbWidth, float bbHeight)
{
	mScreenW = bbWidth;
	mScreenH = bbHeight;
}